const express = require('express');
const router = express.Router();
const bodyParser = require('body-parser');

//10086  到下一个10086间的是我修改的内容
const mysql = require('mysql')
const accountSQL = require('../db/accountSQL');
const appealSQL = require('../db/appealSQL');
const dbConfig = require('../db/dbConfig');
const managerSQL = require('../db/managerSQL');
const messageSQL = require('../db/messageSQL');
const replySQL = require('../db/replySQL');
const sectionSQL = require('../db/sectionSQL');
const topicSQL = require('../db/topicSQL');
const userSQL = require('../db/userSQL');


// 使用DBConfig.js的配置信息创建一个MySql链接池
var pool = mysql.createPool( dbConfig.mysql )
// 响应一个JSON数据
const responseJSON = function (res, ret) {
  if (typeof ret === 'undefined') {
    res.json({
      code: '-200',
      msg: '操作失败'
    })
  } else {
    res.json(ret)
  }
}
//10086

router.post('/addpost', bodyParser.json(), (req, res) => {
  pool.getConnection(function (err, connection) {
    // 检查用户ID是否为空
    if (!req.body["userID"]
      || typeof req.body["userID"] != "number") {
      res.json({
        err: 1,
        message: '用户ID为空'
      })
    }
    // 检查用户名是否为空
    else if (!req.body["username"]
      || typeof req.body["username"] != "string"
      || !req.body["username"].length) {
      res.json({
        err: 1,
        message: '用户名为空'
      })
    } else if (!req.body["title"]
      || typeof req.body["title"] != "string"
      || !req.body["title"].length) {
      res.json({
        err: 1,
        message: '主题为空'
      })
    } else if (!req.body["content"]
      || typeof req.body["content"] != "string"
      || !req.body["content"].length) {
      res.json({
        err: 1,
        message: '内容为空'
      })
    }else if (!req.body["partitionID"]
      || typeof req.body["partitionID"] != "string"
      || !req.body["partitionID"].length) {
      res.json({
        err: 1,
        message: '分区为空'
      })
    } else {
      let userID = req.body["userID"];
      let userName = req.body["userName"];
      let title = req.body["title"];
      let content = req.body["content"];
      let partitionID = req.body["partitionID"];
      // 下方改数据库操作代码
      connection.query(topicSQL.queryAll,function (err,res) {
        var postID = []
        var data = {}
        if (res) {
          for (let i = 0; i < res.length; i++) {
            postId.push(res[i].postID)
          }
        }
        var newPostID = postID[postID.length - 1] + 1
        data.newPostID = newPostID
        connection.query(topicSQL.insert, [tID, tName, tContent, tSecID, tUserID, tLikCount, tWatchCount, tRepCount, tTime],function (err, result) {
          if (result) {
            data.result = {
              code: 200,
              msg: '发表成功'
            }
          } else {
            data.result = {
              code: -1,
              msg: '发表失败'
            }
          }
        })
        if (err) {
          data.err = err
        }
        setTimeout(() => {
          responseJSON(_res, data)
        }, 500)
        connection.release()
      })

      // res.json({
      //   err: 200,
      //   message: '发帖成功',
      //   postID: 123,
      //   userID,
      //   userName,
      //   title,
      //   content,
      //   partitionID,
      //   partitionName: "fenqu1",
      //   partitionTime: 19700101
      // })
    }
  })
})

module.exports = router;
